package com.cdream.basic.algorithm.leetcode.findElement;


import java.util.*;

/**
 * todo
 *
 * @author cdream
 * @date 5/30/21
 */
public class Question49 {

    public static void main(String[] args) {
        System.out.println(new Question49().groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> mp = new HashMap<>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String key = new String(charArray);
            List<String> list = mp.get(key);
            if (list != null) {
                list.add(str);
            } else {
                List<String> objects = new ArrayList<>();
                objects.add(str);
                mp.put(key, objects);
            }
        }
        return new ArrayList<>(mp.values());
    }

}

